#pragma once

#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>

typedef int STDataType;

class stack
{
public:
	void Init()
	{
		_arr = (STDataType*)malloc(sizeof(int) * 4);
		if (_arr == NULL)
		{
			perror("malloc fail\n");
			return;
		}
		top = 0;
		capacity = 4;
	}

	bool Empty()
	{
		return top == 0;
	}

	void Destroy()
	{
		if (!Empty())
		{
			free(_arr);
			_arr = NULL;
			top = capacity = 0;
		}
	}

	void Push(STDataType x)
	{
		if (top == capacity)
		{
			STDataType* tmp = (STDataType*)realloc(_arr, sizeof(STDataType) * capacity * 2);
			if (tmp == NULL)
			{
				perror("realloc fail\n");
				return;
			}

			_arr = tmp;
			capacity *= 2;
		}

		_arr[top] = x;
		top++;
	}

	void Pop()
	{
		if(Empty())
		{
			return;
		}

		top--;
	}

	STDataType Top()
	{
		return _arr[top - 1];
	}

	int Size()
	{
		return top;
	}

private:
	STDataType* _arr;
	int top;
	int capacity;
};